// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <pollux/common/base/succinct_printer.h>

#include <gtest/gtest.h>

namespace kumo::pollux {

TEST(SuccinctPrinterTest, testSuccinctNanos) {
  EXPECT_EQ(succinctNanos(123), "123ns");
  EXPECT_EQ(succinctNanos(1'000), "1.00us");
  EXPECT_EQ(succinctNanos(1'234), "1.23us");
  EXPECT_EQ(succinctNanos(123'456), "123.46us");
  EXPECT_EQ(succinctNanos(1'000'000), "1.00ms");
  EXPECT_EQ(succinctNanos(12'345'678), "12.35ms");
  EXPECT_EQ(succinctNanos(12'345'678, 6), "12.345678ms");
  EXPECT_EQ(succinctNanos(1'000'000'000), "1.00s");
  EXPECT_EQ(succinctNanos(1'234'567'890), "1.23s");
  EXPECT_EQ(succinctNanos(60'499'000'000), "1m 0s");
  EXPECT_EQ(succinctNanos(60'555'000'000), "1m 1s");
  EXPECT_EQ(succinctNanos(3'599'499'000'000), "59m 59s");
  EXPECT_EQ(succinctNanos(3'600'000'000'000), "1h 0m 0s");
  EXPECT_EQ(succinctNanos(86'399'499'000'000), "23h 59m 59s");
  EXPECT_EQ(succinctNanos(86'400'123'000'000), "1d 0h 0m 0s");
  EXPECT_EQ(succinctNanos(867'661'789'000'000), "10d 1h 1m 2s");
  EXPECT_EQ(
      succinctNanos(std::numeric_limits<uint64_t>::max()),
      "213503d 23h 34m 34s");
}

TEST(SuccinctPrinterTest, testSuccinctMicros) {
  EXPECT_EQ(succinctMicros(123), "123us");
  EXPECT_EQ(succinctMicros(1'000), "1.00ms");
  EXPECT_EQ(succinctMicros(1'234), "1.23ms");
  EXPECT_EQ(succinctMicros(59'990), "59.99ms");
  EXPECT_EQ(succinctMicros(60'499), "60.50ms");
  EXPECT_EQ(succinctMicros(61'000), "61.00ms");
  EXPECT_EQ(succinctMicros(3'599'456), "3.60s");
  EXPECT_EQ(succinctMicros(3'600'000), "3.60s");
  EXPECT_EQ(succinctMicros(86'399'498), "1m 26s");
  EXPECT_EQ(succinctMicros(86'400'123), "1m 26s");
  EXPECT_EQ(succinctMicros(867'661'789), "14m 28s");
  EXPECT_EQ(
      succinctMicros(std::numeric_limits<uint64_t>::max()),
      "213503982d 8h 1m 50s");
}

TEST(SuccinctPrinterTest, testSuccinctMillis) {
  EXPECT_EQ(succinctMillis(123), "123ms");
  EXPECT_EQ(succinctMillis(1'000), "1.00s");
  EXPECT_EQ(succinctMillis(1'234), "1.23s");
  EXPECT_EQ(succinctMillis(59'990), "59.99s");
  EXPECT_EQ(succinctMillis(60'499), "1m 0s");
  EXPECT_EQ(succinctMillis(61'000), "1m 1s");
  EXPECT_EQ(succinctMillis(3'599'456), "59m 59s");
  EXPECT_EQ(succinctMillis(3'600'000), "1h 0m 0s");
  EXPECT_EQ(succinctMillis(86'399'498), "23h 59m 59s");
  EXPECT_EQ(succinctMillis(86'400'123), "1d 0h 0m 0s");
  EXPECT_EQ(succinctMillis(867'661'789), "10d 1h 1m 2s");
  EXPECT_EQ(
      succinctMillis(std::numeric_limits<uint64_t>::max()),
      "213503982334d 14h 25m 52s");
}

TEST(SuccinctPrinterTest, testSuccinctBytes) {
  EXPECT_EQ(succinctBytes(123), "123B");
  EXPECT_EQ(succinctBytes(1'024), "1.00KB");
  EXPECT_EQ(succinctBytes(123'456), "120.56KB");
  EXPECT_EQ(succinctBytes(1'048'576), "1.00MB");
  EXPECT_EQ(succinctBytes(12'345'678, 4), "11.7738MB");
  EXPECT_EQ(succinctBytes(1'073'741'824), "1.00GB");
  EXPECT_EQ(succinctBytes(1'234'567'890), "1.15GB");
  EXPECT_EQ(succinctBytes(1'099'511'627'776), "1.00TB");
  EXPECT_EQ(succinctBytes(1234'099'511'627'776), "1122.41TB");
  EXPECT_EQ(
      succinctBytes(std::numeric_limits<uint64_t>::max()), "16777216.00TB");
}

} // namespace kumo::pollux
